package com.css.openapi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.css.common.core.constant.Result;
import com.css.common.core.exception.ServiceException;
import com.css.common.core.session.SessionUtils;
import com.css.openapi.BO.FxOrderInvoiceResponse;
import com.css.openapi.BO.SimpleBooleanResponse;
import com.css.openapi.BO.fxdw.FxOrderInvoiceBO;
import com.css.openapi.BO.fxdw.FxOrderQueryBO;
import com.css.openapi.entity.ZfjkpFxkpInvoice;
import com.css.openapi.feign.ZfjkpPtCommonClient;
import com.css.openapi.mapper.ZfjkpFxkpInvoiceMapper;
import com.css.openapi.service.ZfjkpFxkpInvoiceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
* @author 21062
* @description 针对表【zfjkp_fxkp_invoice(发票信息表)】的数据库操作Service实现
* @createDate 2025-10-10 16:12:26
*/
@Service
public class ZfjkpFxkpInvoiceServiceImpl extends ServiceImpl<ZfjkpFxkpInvoiceMapper, ZfjkpFxkpInvoice>
    implements ZfjkpFxkpInvoiceService{

    @Resource
    private ZfjkpPtCommonClient zfjkpPtCommonClient;

    @Override
    @Transactional
    public FxOrderInvoiceBO fxQueryOrderInvoice(FxOrderQueryBO fxOrderQueryBO) {
        FxOrderInvoiceResponse response = zfjkpPtCommonClient.queryOrderInvoice(fxOrderQueryBO);
        if(!Objects.equals(0,response.getCode()))throw new ServiceException(response.getMsg());
        final ZfjkpFxkpInvoice invoice = new ZfjkpFxkpInvoice();
        BeanUtil.copyProperties(response,invoice);
        invoice.setUuid(IdUtil.fastSimpleUUID());
        invoice.setCjr(SessionUtils.getCjr());
        invoice.setCjsj(new Date());
        this.save(invoice);
        if ("hzfp".equals(response.getFplx())){
            ZfjkpFxkpInvoice lzfp = this.lambdaQuery()
                    .eq(ZfjkpFxkpInvoice::getDdbh,fxOrderQueryBO.getDdbh())
                    .eq(ZfjkpFxkpInvoice::getFplx,"lzfp").one();
            lzfp.setFphmHp(response.getFphm());
            lzfp.setHcbz("Y");
            lzfp.setXgr(SessionUtils.getCjr());
            lzfp.setXgsj(new Date());
            this.updateById(lzfp);
        }
        FxOrderInvoiceBO fxOrderInvoiceBO = new FxOrderInvoiceBO();
        BeanUtil.copyProperties(response,fxOrderInvoiceBO);
        return fxOrderInvoiceBO;
    }

    @Override
    @Transactional
    public Boolean orderRed(FxOrderQueryBO fxOrderQueryBO) {
        SimpleBooleanResponse response = zfjkpPtCommonClient.orderRed(fxOrderQueryBO);
        if(!Objects.equals(0,response.getCode()))throw new ServiceException(response.getMsg());
        fxQueryOrderInvoice(fxOrderQueryBO);
        return response.getData();
    }
}




